package com.example.innovate.service.impl;

import com.example.innovate.domain.JobWantedDO;
import com.example.innovate.domain.JobWantedDOExample;
import com.example.innovate.mapper.JobWantedDOMapper;
import com.example.innovate.service.JobWantedService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JobWantedServiceImpl implements JobWantedService {
    @Autowired
    JobWantedDOMapper jobWantedDOMapper;

    @Override
    public JobWantedDO addInfo(JobWantedDO jobWantedDO) {
        jobWantedDOMapper.insertSelective(jobWantedDO);
        return jobWantedDO;
    }

    @Override
    public void deleteInfo(int id) {
        jobWantedDOMapper.deleteByPrimaryKey(id);
    }

    @Override
    public JobWantedDO updateInfo(JobWantedDO jobWantedDO) {
        jobWantedDOMapper.updateByPrimaryKeySelective(jobWantedDO);
        return jobWantedDO;
    }

    @Override
    public JobWantedDO findByName(String name) {
        JobWantedDOExample jobWantedDOExample = new JobWantedDOExample();
        jobWantedDOExample.createCriteria().andJobNameEqualTo(name);
        List<JobWantedDO> list = jobWantedDOMapper.selectByExample(jobWantedDOExample);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<JobWantedDO> findInfo(int pageNumber, int pageSize,
                                      String graSchool, String major,
                                      String company, String position,
                                      String firstWorkPlace, int userId) {
        JobWantedDOExample jobWantedDOExample = new JobWantedDOExample();
        JobWantedDOExample.Criteria criteria = jobWantedDOExample.createCriteria();
        PageHelper.startPage(pageNumber,pageSize);
        if(graSchool!=null){
            criteria.andJobGraSchoolEqualTo(graSchool);
        }
        if(major!=null){
            criteria.andJobMajorEqualTo(major);
        }
        if(company!=null){
            criteria.andJobCompanyEqualTo(company);
        }
        if(position!=null){
            criteria.andJobPositionEqualTo(position);
        }
        if(firstWorkPlace!=null){
            criteria.andJobFirstWorkPlaceEqualTo(firstWorkPlace);
        }
        if(userId!=0){
            criteria.andJobUserIdEqualTo(userId);
        }
        List<JobWantedDO> list = jobWantedDOMapper.selectByExample(jobWantedDOExample);
        return list;
    }

    @Override
    public JobWantedDO findById(int id) {
        return jobWantedDOMapper.selectByPrimaryKey(id);
    }
}
